<template>
  <div class="register-container min-h-screen flex justify-center items-center bg-light-bg-primary dark:bg-dark-bg-primary blue:bg-blue-bg-primary transition-colors duration-300">
    <div class="register-form-wrapper w-[400px] sm:w-[90%] max-w-[400px] p-8 bg-light-bg-secondary dark:bg-dark-bg-secondary blue:bg-blue-bg-secondary rounded-xl shadow-lg border border-light-border dark:border-dark-border blue:border-blue-border transition-colors duration-300">
      <div class="register-header text-center mb-8">
        <h2 class="text-2xl font-bold text-accent-color mb-2">新易访内网系统</h2>
        <p class="text-light-text-secondary dark:text-dark-text-secondary blue:text-blue-text-secondary">创建您的账号</p>
      </div>
      
      <el-form 
        :model="registerForm"
        :rules="registerRules"
        ref="registerFormRef"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            class="bg-light-bg-primary dark:bg-dark-bg-primary blue:bg-blue-bg-primary border-light-border dark:border-dark-border blue:border-blue-border"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            class="bg-light-bg-primary dark:bg-dark-bg-primary blue:bg-blue-bg-primary border-light-border dark:border-dark-border blue:border-blue-border"
          />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            prefix-icon="Lock"
            show-password
            class="bg-light-bg-primary dark:bg-dark-bg-primary blue:bg-blue-bg-primary border-light-border dark:border-dark-border blue:border-blue-border"
          />
        </el-form-item>
        
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="registerForm.name"
            placeholder="请输入您的姓名"
            prefix-icon="UserFilled"
            class="bg-light-bg-primary dark:bg-dark-bg-primary blue:bg-blue-bg-primary border-light-border dark:border-dark-border blue:border-blue-border"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleRegister"
            class="register-btn w-full h-10 text-base bg-accent-color hover:bg-accent-color/90 text-white"
          >
            {{ loading ? '注册中...' : '注册' }}
          </el-button>
        </el-form-item>
        
        <el-form-item>
          <div class="login-link text-center mt-4">
            <span class="text-light-text-secondary dark:text-dark-text-secondary blue:text-blue-text-secondary">已有账号？</span>
            <router-link to="/login" class="text-accent-color hover:text-accent-color/80 ml-1 transition-colors">立即登录</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useAuthStore } from '../../stores/auth'
import { useThemeStore } from '../../stores/theme'
import { ElMessage } from 'element-plus'
import router from '../../router'

// 使用auth store
const authStore = useAuthStore()
const themeStore = useThemeStore()

// 表单引用
const registerFormRef = ref()

// 加载状态
const loading = ref(false)

// 注册表单数据
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  name: ''
})

// 注册表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3到20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' },
    { 
      pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,20}$/, 
      message: '密码必须包含字母和数字', 
      trigger: 'blur'
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (_: any, value: string, callback: any) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 20, message: '姓名长度应在2到20个字符之间', trigger: 'blur' }
  ]
}

// 处理注册
const handleRegister = async () => {
  try {
    // 表单验证
    await registerFormRef.value?.validate()
    
    // 设置加载状态
    loading.value = true
    
    // 调用注册方法
    const success = await authStore.register(
      registerForm.username,
      registerForm.password,
      registerForm.name
    )
    
    if (success) {
      ElMessage.success('注册成功，请登录')
      router.push('/login')
    } else {
      ElMessage.error(authStore.error || '注册失败')
    }
  } catch (error) {
    console.error('Register validation error:', error)
  } finally {
    loading.value = false
  }
}

// 组件挂载时初始化主题
onMounted(() => {
  themeStore.initTheme()
})
</script>

<style scoped>
/* 确保组件内所有元素都有过渡效果 */
* {
  transition-property: color, background-color, border-color, text-decoration-color, fill, stroke;
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  transition-duration: 300ms;
}

html, body, #app {
  height: 100%;
  margin: 0;
  padding: 0;
}

/* 响应式调整已通过Tailwind的响应式类实现 */
</style>